﻿using System.Collections.Generic;
using System.Linq;
using System.Text;
using Pasys.PresentationCore;
using UIShell.OSGi;
using System.Windows.Forms;
using Pasys.PresentationCore.DockShell;
using Pasys.PresentationCore.DockShell.SmartPartInfos;
using GeneratorLib.DB.Schemas;
using GeneratorLib.DB;
using System;
using GeneratorLib;

namespace Generator.Pasys
{
    public class GenetorBaseProvider<T> : AbstractViewProvider where T :Form, IGenerator, new()
    {

        private GeneratorLib.DB.IConnection Connection { get; set; }
        private GeneratorLib.DB.Schemas.TableInfo Table { get; set; }

        public GenetorBaseProvider()
            : base(false)
        {
            var Connection = BundleRuntime.Instance.GetFirstOrDefaultService<IConnection>();
            var Table = (TableInfo)BundleRuntime.Instance.GetFirstOrDefaultService<ITableInfo>();
        }

        protected override Control CreateControl()
        {
            var c = BundleRuntime.Instance.GetFirstOrDefaultService<IConnection>();
            if (c == null)
            {
                c = Connection;
            }
            else
            {
                Connection = c;
            }
            //if (c != null)
            //{
            //    BundleRuntime.Instance.RemoveService<IConnection>(c);
            //}

            var t = (TableInfo)BundleRuntime.Instance.GetFirstOrDefaultService<ITableInfo>();
            if (t == null)
            {
                t = Table;
            }
            else
            {
                Table = t;
            }
            //if (t != null)
            //{
            //    BundleRuntime.Instance.RemoveService<ITableInfo>(t);
            //}

            var f = new T()
            {
                Connection = c,
                Table = t
            };
            return f;
        }

        public override string GetPersistParam()
        {
            StringBuilder sb= new StringBuilder();
            sb.AppendLine(string.Format("ProviderName={0}", Connection.DBProvider.DBProviderName));
            sb.AppendLine(string.Format("Name={0} Conn={1}", Connection.Name, Connection.ConnectStr));
            sb.AppendLine(string.Format("TableName={0}", Table.TableName));

            return sb.ToString();
        }

        public override void SetPersistParam(string param)
        {
            if (string.IsNullOrEmpty(param))
            {
                return;
            }

            System.IO.StringReader sr = new System.IO.StringReader(param);
            IDBProvider provider = null;
            while(sr.Peek()>0)
            {
                var str=sr.ReadLine();
                if (str.StartsWith("ProviderName="))
                {
                    var providerName = str.Substring(13);
                    provider = GetProviderByName(providerName);
                }
                else if (str.StartsWith("Name="))
                {
                    var idx= str.IndexOf("Conn=");
                    var name = str.Substring(5,idx-5);
                    var conn = str.Substring(idx + 5);
                    Connection = null; // new GeneratorLib.DB.Connectios.SQLServer2008Connection(name, conn);
                }
                else if (str.StartsWith("TableName="))
                {
                    var name = str.Substring(10);
                    if (Connection != null)
                    {
                        Table = Connection.FindTable(name.Trim());
                    }
                }
            }
        }


        private IDBProvider GetProviderByName(string providerName)
        {
            var provideManager = BundleRuntime.Instance.GetFirstOrDefaultService<IDBProviderManager>();
            var provider = provideManager.DBProviders.First(p => p.DBProviderName.Equals(providerName));
            return provider;
        }


        protected override ISmartPartInfo CreateViewInfo()
        {
            //Assembly assembly = Assembly.GetExecutingAssembly();
            //System.Drawing.Icon icon = null;

            //using (var stream = assembly.GetManifestResourceStream("DBExplorer.Resources.Hyperlink16.png"))
            //{                
            //    icon = new System.Drawing.Icon (stream, 16, 16);
            //}

            return new DockPanelSmartPartInfo()
            {
                //Icon = icon,
                DockingType = DockingType.Document,
                ShowStrategy = new DockStateShowStrategy(DockState.Document)
            };
        }

  
    }
}
